/**
 * Copyright (c) CSIRO Australia, 2009
 *
 * @author $Author: jia020 $
 * @version $Id: LoginPanel.java 643 2009-05-20 00:20:55Z jia020 $
 */
package au.csiro.nt.pdsp.client;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.TextField;

/**
 * Class to create LoginPanel. Allow user to input username and password. Verify
 * username and password by submitting the form to server.
 */
public class LoginPanel extends Panel {
	/**
	 * eventHandler for callback.
	 */
	EventHandler eventHandler = null;
	/**
	 * service point for rpc call. 
	 */		
	private final PDQueryServiceAsync service = GWT.create(PDQueryService.class);

	/**
	 * Constructor to create LoginPanel.
	 * 
	 * @param eventHandler
	 *          eventHandler for successfully pass the user authentication.
	 */
	public LoginPanel(final EventHandler eventHandler) {
		this.eventHandler = eventHandler;
		this.setBorder(false);
		this.setPaddings(15);
		// create the form
		final FormPanel formPanel = new FormPanel();
		formPanel.setTitle("Input your user infomation");
		formPanel.setFrame(true);
		formPanel.setPaddings(5, 5, 5, 5);
		formPanel.setWidth(250);
		// formPanel.setHeight(550);
		formPanel.setLabelWidth(75);
		formPanel.setUrl("save-form.php");
		formPanel.addClass("LoginPanel");
		// FieldSet inputFS = new FieldSet("Input");
		Label lbTED = new Label(
				"Please make sure your TED \nwas plugged in before Login.");

		TextField user = new TextField("User", "user", 80);
		user.setAllowBlank(false);
		user.setName("user");

		TextField password = new TextField("Password", "password", 80);
		password.setAllowBlank(false);
		password.setInputType("password");
		password.setName("password");

		// inputFS.add(user);
		// inputFS.add(password);

		Button btLogin = new Button("Login");
		btLogin.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				Form form = formPanel.getForm();
				String formValues = form.getValues();
				String[] nameValuePairs = formValues.split("&");
				Map loginData = new HashMap();
				for (int i = 0; i < nameValuePairs.length; ++i) {
					String[] oneItem = nameValuePairs[i].split("=");
					if (oneItem.length == 2)
						loginData.put(oneItem[0], oneItem[1]);
				}
				final String username = (String) loginData.get("user");
				String password = (String) loginData.get("password");
				if (username == null || password == null || username.isEmpty()
						|| password.isEmpty())
					return;
				service.login(username, password, new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						MessageBox.alert("failed:Login");
					}

					@Override
					public void onSuccess(String result) {
						// TODO Auto-generated method stub
						String params = result;
						if (params == null || params.isEmpty()) {
							MessageBox.alert("failed:Login");
						} else {
							ParamsParser parser = new ParamsParser();
							if (parser.loadParams(params) > 0) {
								GlobalVars.getInstance().userid = parser
										.getParam(PdspConst.XMLPARAM_USERID);
								GlobalVars.getInstance().role = parser
										.getParam(PdspConst.XMLPARAM_ROLE);
								GlobalVars.getInstance().status = parser
										.getParam(PdspConst.XMLPARAM_STATUS);
								showOperationPanel();
							}
						}
					}
				});
			}
		});
		
		formPanel.add(lbTED);
		formPanel.add(user);
		formPanel.add(password);
		formPanel.addButton(btLogin);

		this.add(formPanel);

		new Viewport(this);
	}

	public void showOperationPanel() {
		eventHandler.processEvent(PdspConst.EVENT_SHOW_OPERATION, "");
	}
}